package com.peace.hospital.opdept.mapper.common;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.peace.hospital.entity.common.People;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * <p>
 * 病人表 Mapper 接口
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-11-11
 */
@Mapper
public interface PeopleMapper extends BaseMapper<People> {

    /**添加病人信息*/
    @Insert("insert into hospital_public.people(pname,pcode,pgender) values(#{pname},#{pcode},#{pgender})")
    int addPeople(People people);

    /**按照身份证查询病人信息*/
    @Select("select * from hospital_public.people where pcode=#{pcode}")
    People selectPeopleByPcode(String pcode);

    /**按照用户名或者身份证查询病人信息*/
    @SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByNameOrCode")
    List<People> getPeopleByNameOrCode(People people);

    class UserSqlBuilder {
        public static String buildGetUsersByNameOrCode(final People people) {
            return new SQL(){{
                SELECT("*");
                FROM("hospital_public.people");
                if (people.getPname() != null) {
                    WHERE("pname LIKE CONCAT('%',#{pname},'%')");
                }else if(people.getPcode() != null) {
                    WHERE( "pcode LIKE CONCAT('%',#{pcode},'%')");
                }
            }}.toString();
        }
    }
}
